package com.zdsoft.blp.gongyong.net;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.zdsoft.blp.gongyong.upLoad.Base64ToString;
import com.zdsoft.blp.gongyong.upLoad.EncodeBase64ToString;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2016/9/13.
 */
public class UpLoadFiles {

    public static void upLoadFiles(final List<Bitmap> bitmaps, final Handler handler, final String path) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                Net net = new Net();
                net.url = "http://114.215.193.46:8080/services/uploadFileService";
                net.nameSpace = "http://file.service.zhidisoft.com";
                net.methodName = "uploadFile";
                StringBuffer tempFileIds = new StringBuffer();
                for (int i = 0; i < bitmaps.size(); i++) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("fileName", "qwe.jpg");
                    map.put("file", Base64ToString.toString(bitmaps.get(i)));
                    net.parms = map;
                    String res = WebUtil.getResult(net);
                    try {
                        tempFileIds.append(new JSONObject(res).get("tempFileId"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                if(!"".equals(path)&&path!=null){
                    Map<String, Object> map = new HashMap<>();
                    map.put("fileName", "zxc.amr");
                    map.put("file", EncodeBase64ToString.toString(path));
                    net.parms = map;
                    String res = WebUtil.getResult(net);
                    try {
                        tempFileIds.append(new JSONObject(res).get("tempFileId"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                Message msg = handler.obtainMessage();
                msg.what = 2;
                msg.obj = tempFileIds;
                handler.sendMessage(msg);
            }
        }.start();
    }
}
